package com.yifang.yibingproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifang.yibingproject.entity.SysMenuEntity;
import com.yifang.yibingproject.entity.SysRoleMenuEntity;
import com.yifang.yibingproject.mapper.SysRoleMenuMapper;
import com.yifang.yibingproject.service.SysMenuService;
import com.yifang.yibingproject.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 根据角色ID获取菜单集合
     * @param id
     * @return
     */
    @Override
    public Set<Long> getMenuIdByRoleId(Long id) {
        if (id == null || id <= 0){
            return new HashSet<>();
        }
        LambdaQueryWrapper<SysRoleMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenuEntity::getRoleId,id);
        List<SysRoleMenuEntity> roleMenuList = list(queryWrapper);
        Set<Long> menuId =  roleMenuList.stream().map(SysRoleMenuEntity::getMenuId).collect(Collectors.toSet());

        List<SysMenuEntity> list = sysMenuService.getMenuByIds(menuId);
        Set<Long> ids = list.stream().map(SysMenuEntity::getId).collect(Collectors.toSet());
        return ids;
    }

}
